/*
Mem.Get("key"); //获取值
Mem.Put("key","值支持对象更新部分","时间"); //更新值 可以只更新时间 时间0不过期 值不存在返回 false
Mem.Set(key,value,"时间");//设置或者新增值 
Mem.Add(key,"默认值1",time);//增加值  支持时间过期
Mem.Cut(key,"默认值1",time);//减少值 支持时间过期
DMem.Del(key);//删除
DMem.Flush(key);// key 为空清理全部  不为空删除包含关键词
*/
var Kuo_Cache_FileCache = {
    DirsTotalnum: 0,
    DB:null,
    Mulu:{},
    Ext:"db",
    GUoLV:(name)=>{
        if(!name){
            return name;
        }
        name = ""+name;
        return name.replace(/\./g,'');
    },Getfile:(filePath)=>{
        var neirong = null;
        try {
            neirong = FS.readFileSync(filePath).toString();
        } catch (err) {
            neirong = null;
        }
        return neirong;
    },
    Isfile:(key,filenm,chuangjian)=>{
        if(!key){
            return false;
        }
        key = Kuo_Cache_FileCache.GUoLV(key);
        var mulu = key.split("/");
        if(mulu.length > 1){
            mulu.pop();
            var dirmulu = mulu.join("/")+'/';
            if(!Kuo_Cache_FileCache.Mulu[dirmulu] || chuangjian){
                Kuo_Cache_FileCache.Mulu[dirmulu] = 1;
                MkDirs(temp+dirmulu);
            }
        }
        var file = temp+key+Kuo_Cache_FileCache.Ext+'.Kuo';
        if(!filenm){
            return file;
        }
        var jeiguo =Kuo_Cache_FileCache.Getfile(file);
        if(jeiguo){
            return JSON.parse(jeiguo);
        }else{
            return null;
        }
    },
    //配置参数 Mode("Tools").Time()
    Config:(config)=>{
        if(config && config.dbname){
            Kuo_Cache_FileCache.Ext = "."+Kuo_Cache_FileCache.GUoLV(config.dbname);
        }
        return Kuo_Cache_FileCache;
    },Get:(key)=>{
        /*获取数据 */
        var file = Kuo_Cache_FileCache.Isfile(key,true);
        if(file){
            if(file[1] > 0 && Mode("Tools").Time() > file[1]){
                Kuo_Cache_FileCache.Del(key);
                return null;
            }
            return file[0];
        }else{

            return null;
        }

    },Put:async(key,value,time)=>{
        /*更新*/
        if(!key){
            return false;
        }
        if(!time){
            time = 0;
        }
        if(value){
            var zhi =  Kuo_Cache_FileCache.Get(key);
            if(zhi && typeof(zhi) == typeof(value)){
                for(var k in value){
                    zhi[k] = value[k];
                }
                value = zhi;
            }
        }
        return  Kuo_Cache_FileCache.Set(key,value,time);
       
    },Set:async (key,value,time)=>{
        /**设置值  */
        if(!key){
            return false;
        }
        if(!time){
            time = 0;
        }
        if(time > 0){
            time += Mode("Tools").Time();
        }
        var shuju =[];
        var file = Kuo_Cache_FileCache.Isfile(key,false,true);
        if(value){
            shuju =[value,time];
        }else{
            shuju = Kuo_Cache_FileCache.Isfile(key,true);
            if(shuju){
                shuju =[shuju[0],time];
            }else{
               shuju =[null,time];
            }
        }

        try {
            FS.writeFileSync(file,JSON.stringify(shuju));
            return true;
        } catch (error) {
            return false;
        }
       

    },Add:async(key,value,time)=>{
        /* 增加 */
        if(!time){
            time = 0;
        }
        if(!value){
            value = 1;
        } 
        var zhi = Kuo_Cache_FileCache.Get(key);
        if(!zhi){
            zhi = 0;
        }
        if(isNaN(zhi)){
            zhi = 0;
        }
        zhi = Number(zhi*1);
        zhi += Number(value);
        Kuo_Cache_FileCache.Set(key,zhi,time);
        return zhi;
    },Cut:async(key,value,time)=>{
        /* 减少 */
        if(!time){
            time = 0;
        }
        if(!value){
            value = 1;
        } 
        var zhi = Kuo_Cache_FileCache.Get(key);
        if(!zhi){
            zhi = 0;
        }
        if(isNaN(zhi)){
            zhi = 0;
        }
        zhi = Number(zhi*1);
        zhi -= Number(value);
        Kuo_Cache_FileCache.Set(key,zhi,time);
        return zhi;
    },Del:async(key)=>{
        /*删除缓存 */
        if(!key){
            return false;
        }
        var file = Kuo_Cache_FileCache.Isfile(key,false);
        try {
            FS.unlinkSync(file);
        } catch (error) {}
        return true;

    },DirsTotal:(path) =>{
        let files = [];
        if(FS.existsSync(path)){
            files = FS.readdirSync(path);
            files.forEach((file, index) => {
                let curPath = path + "/" + file;
                if(FS.statSync(curPath).isDirectory()){
                    Kuo_Cache_FileCache.DirsTotal(curPath);
                } else {
                    if(file != '.DS_Store'){
                        Kuo_Cache_FileCache.DirsTotalnum++;
                    }
                }
            });
        }
    },Total:async()=>{
        /*统计数据 */
        Kuo_Cache_FileCache.DirsTotal(temp);
        var jishi = Kuo_Cache_FileCache.DirsTotalnum*1;
        Kuo_Cache_FileCache.DirsTotalnum = 0;
        return jishi;
    },Flush:async(key)=>{
        /*清空数据*/
        key = Kuo_Cache_FileCache.GUoLV(key);
        if(key){
            DelDirs(temp+key+"/");
            key = RTrim(key,'/');
            var mulu = Kuo_Cache_FileCache.Mulu;
            for(var k in  mulu){
                if(k.indexOf(key)>-1){
                    delete Kuo_Cache_FileCache.Mulu[k];
                }
            }
        }else{
            Kuo_Cache_FileCache.Mulu ={};
            DelDirs(temp);
            MkDirs(temp);
        }
        return true;
    },Save:async()=>{
        /* 预留接口存档 */
        return true;
    }
};
module.exports = Kuo_Cache_FileCache;